import { reqgetbanner, reqgetgroupon, reqgetindexgoods, reqgetnew, reqgetseckill } from "../../http/api"

// state 
const initState = {
    banner: [],
    newList: [],
    groupon: [],
    seckill: [],
    indexGoods: [],
    n: 0
}

// action type
const TYPES = {
    HOME_GET_BANNER: "HOME_GET_BANNER",
    HOME_GET_NEWLIST: "HOME_GET_NEWLIST",
    HOME_GET_GROUPON: "HOME_GET_GROUPON",
    HOME_GET_SECKILL: "HOME_GET_SECKILL",
    HOME_GET_INDEXGOODS: "HOME_GET_INDEXGOODS",
    HOME_CHANGE_N: "HOME_CHANGE_N"
}

// reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.HOME_GET_BANNER:
            return {
                ...state,
                banner: action.banner
            }
        case TYPES.HOME_GET_NEWLIST:
            return {
                ...state,
                newList: action.newList
            }
        case TYPES.HOME_GET_GROUPON:
            return {
                ...state,
                groupon: action.groupon
            }
        case TYPES.HOME_GET_SECKILL:
            return {
                ...state,
                seckill: action.seckill
            }
        case TYPES.HOME_GET_INDEXGOODS:
            return {
                ...state,
                indexGoods: action.indexGoods
            }
        case TYPES.HOME_CHANGE_N:
            return {
                ...state,
                n: action.n
            }
        default:
            return state
    }
}

// action creator
export const actions = {
    changeBanner: banner => ({ type: TYPES.HOME_GET_BANNER, banner }),
    reqBanner: () => (dispatch, getState) => {
        console.log(getState());
        let banner = getBanner(getState());
        if (banner.length > 0) {
            return
        }
        reqgetbanner().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeBanner(res.data.list))
            }
        })
    },
    changeNewList: newList => ({ type: TYPES.HOME_GET_NEWLIST, newList }),
    reqNew: () => (dispatch, getState) => {
        let newList = getNew(getState());
        if (newList.length > 0) {
            return
        }
        reqgetnew().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeNewList(res.data.list))
            }
        })
    },
    changeGroupOn: groupon => ({ type: TYPES.HOME_GET_GROUPON, groupon }),
    reqGroupOn: () => (dispatch, getState) => {
        let groupon = getGroupOn(getState());
        if (groupon.length > 0) {
            return
        }
        reqgetgroupon().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeGroupOn(res.data.list))
            }
        })
    },
    changeSeckill: seckill => ({ type: TYPES.HOME_GET_SECKILL, seckill }),
    reqSeckill: () => (dispatch, getState) => {
        let seckill = getSeckill(getState());
        if (seckill.length > 0) {
            return
        }
        reqgetseckill().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeSeckill(res.data.list))
            }
        })
    },
    changeIndexGoods: indexGoods => ({ type: TYPES.HOME_GET_INDEXGOODS, indexGoods }),
    reqIndexGoods: () => (dispatch, getState) => {
        let n = getN(getState())
        reqgetindexgoods().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeIndexGoods(res.data.list[n].content))
            }
        })
    },
    changeN: n => ({ type: TYPES.HOME_CHANGE_N, n }),
    newGoods: (n) => (dispatch, getState) => {
        dispatch(actions.changeN(n))
        dispatch(actions.reqIndexGoods())
    }

}

// selector导出数据给组件
export const getBanner = state => state.home.banner
export const getNew = state => state.home.newList
export const getGroupOn = state => state.home.groupon
export const getSeckill = state => state.home.seckill
export const getIndexGoods = state => state.home.indexGoods
export const getN = state => state.home.n

// 导出reducer
export default reducer